package development.perpective;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import util.Globals;
import DTO.Aspect;

public class ExportArchive {

	FileWriter fileWriter;
	BufferedWriter bufferedWriter;
	PrintWriter printWriter;

	/**
	 * Join all aspect components on a String.
	 */
	public String joinAspectComponents(Aspect aspect){
		String joinedAspectComponents = "";

		// aspect head
		joinedAspectComponents = Globals.ASPECT_NAME + " " + aspect.getAspectName() /*+ " : " + aspect.getPropertyAssociatedName() TODO*/;
		
		// l_brace
		joinedAspectComponents = joinedAspectComponents + " { ";
		
		// intertypes
		joinedAspectComponents = joinedAspectComponents + "\n\n" + aspect.getIntertypesCode();
		
		// advices and pointcuts
		joinedAspectComponents = joinedAspectComponents + "\n\n" + aspect.getAdvicesAndPointcutsCode();
		
		// r_brace
		joinedAspectComponents = joinedAspectComponents + "\n\n" +  " } ";
		
		return joinedAspectComponents;
	}

	/**
	 * Creates a archive of aspect components for validate it with compiler.
	 */
	public String exportAspectArchive(String aspectCode){
		try {
			fileWriter = new FileWriter(Globals.DEFAULT_PATH_OF_SAVE_GENERATED_FILE);
			bufferedWriter = new BufferedWriter(fileWriter);
			printWriter = new PrintWriter(bufferedWriter);

			printWriter.print(aspectCode);

			printWriter.flush();
			printWriter.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		return Globals.DEFAULT_PATH_OF_SAVE_GENERATED_FILE;
	}
}
